<?php
session_start();
require_once '../config.php';
require_once '../functions.php';

// 如果已经登录，跳转到管理首页
if (is_admin_logged_in()) {
    header('Location: index.php');
    exit;
}

$message = '';
$message_type = '';

// 处理登录请求
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = trim($_POST['username'] ?? '');
    $password = trim($_POST['password'] ?? '');
    
    if (empty($username) || empty($password)) {
        $message = '请输入用户名和密码';
        $message_type = 'error';
    } elseif (admin_login($username, $password)) {
        header('Location: index.php');
        exit;
    } else {
        $message = '用户名或密码错误';
        $message_type = 'error';
    }
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>管理员登录 - <?php echo SITE_NAME; ?></title>
    <link rel="stylesheet" href="../static/css/bootstrap.min.css">
    <link rel="stylesheet" href="../static/css/all.min.css">
    <style>
        body {
            background-color: #fff5f8;
            font-family: 'Arial', sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            margin: 0;
            padding: 20px;
        }
        
        .login-container {
            background-color: white;
            border-radius: 15px;
            padding: 40px;
            box-shadow: 0 5px 20px rgba(0,0,0,0.1);
            width: 100%;
            max-width: 400px;
        }
        
        .login-title {
            color: #ff69b4;
            text-align: center;
            margin-bottom: 30px;
            font-size: 1.8rem;
        }
        
        .form-group {
            margin-bottom: 25px;
        }
        
        .form-label {
            display: block;
            margin-bottom: 8px;
            color: #d63384;
            font-weight: bold;
        }
        
        .form-control {
            width: 100%;
            padding: 12px;
            border: 1px solid #ffccd5;
            border-radius: 8px;
            font-size: 1rem;
        }
        
        .form-control:focus {
            border-color: #ff69b4;
            outline: none;
            box-shadow: 0 0 0 3px rgba(255, 105, 180, 0.2);
        }
        
        .btn-login {
            width: 100%;
            padding: 12px;
            background-color: #ff69b4;
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 1.1rem;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        
        .btn-login:hover {
            background-color: #d63384;
        }
        
        .message {
            padding: 15px;
            border-radius: 8px;
            margin-bottom: 20px;
            text-align: center;
        }
        
        .message-error {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        
        .site-link {
            text-align: center;
            margin-top: 20px;
        }
        
        .site-link a {
            color: #ff69b4;
            text-decoration: none;
        }
        
        .site-link a:hover {
            text-decoration: underline;
            color: #d63384;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <h1 class="login-title"><i class="fas fa-lock"></i> 管理员登录</h1>
        
        <?php if (!empty($message)): ?>
            <div class="message message-<?php echo $message_type; ?>">
                <?php echo $message; ?>
            </div>
        <?php endif; ?>
        
        <form method="post">
            <div class="form-group">
                <label class="form-label" for="username">用户名</label>
                <input type="text" class="form-control" id="username" name="username" required>
            </div>
            
            <div class="form-group">
                <label class="form-label" for="password">密码</label>
                <input type="password" class="form-control" id="password" name="password" required>
            </div>
            
            <button type="submit" class="btn-login">登录</button>
        </form>
        
        <div class="site-link">
            <a href="../index.php">返回网站首页</a>
        </div>
    </div>
</body>
</html>
